package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.ProductBean;
import com.project.bean.ShoppingCartBean;
import com.project.mapper.IOrderDetailMapper;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShoppingCartMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImp implements IOrderService {
    @Autowired
    private  IOrderMapper orderMapper;
    @Autowired
    private IOrderDetailMapper orderDetailMapper;
    @Autowired
    private IShoppingCartMapper shoppingCartMapper;

    @Override
    public void addOrder(OrderBean orderBean) {
        //添加订单
        orderMapper.insert(orderBean);
        //按用户id查询购物车集合
        QueryWrapper<ShoppingCartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",orderBean.getUserId());
        List<ShoppingCartBean> shopList =shoppingCartMapper.selectList(qw);
        //批量添加订单明细
        orderDetailMapper.addDetails(orderBean.getId(),shopList);
        //清空购物车
        shoppingCartMapper.delete(qw);
    }

    @Override
    public IPage<OrderBean> findByItem(Integer pageNO, Integer userId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        return orderMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public OrderBean findById(Integer orderId) {
        return orderMapper.findById(orderId);
    }

    @Override
    public List<OrderBean> showAll() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("o_status","发货中");
        return orderMapper.selectList(qw);
    }

    @Override
    public void updateStatus(Integer orderId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("pk_orderId",orderId);
        OrderBean orderBean=orderMapper.selectOne(qw);
        orderBean.setStatus("已完结");
        orderMapper.updateById(orderBean);
    }

    @Override
    public IPage<ProductBean> cutByItem(Integer pageNO, String status, LocalDate startDate, LocalDate endDate) {
        QueryWrapper qw = new QueryWrapper<>();
        if(status!=null&&status.length()!=0&& !status.equals("不限")){
            qw.eq("o_status",status);
        }
        if(startDate!=null){
            qw.ge("o_date",startDate);
        }
        if(endDate!=null){
            qw.le("o_date",endDate);
        }
        return orderMapper.selectPage(new Page(pageNO,3),qw);
    }

    @Override
    public void managerUpdateStatus(Integer orderId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("pk_orderId",orderId);
        OrderBean orderBean=orderMapper.selectOne(qw);
        orderBean.setStatus("发货中");
        orderMapper.updateById(orderBean);
    }
}
